package com.alibaba.agile;

import java.util.HashMap;
import java.util.Map;

public class PaymentService {
	public static Map<String, Double> discountMap = new HashMap<String, Double>();
	static {
		discountMap.put("GOLDENROLE", 0.8);
		discountMap.put("SILIVERROLE", 0.9);
		discountMap.put("COMMONROLE", 1.0);

	}

	public double payment(double total, double givenPay) {
		return givenPay - total;
	}

	public double sumPay(BookBasket basket, String role) {

		double discount = getDiscount(role);
		double sum = 0;
		for (BookItem item : basket.getBookItemList()) {
			sum += item.getBook().getPrice() * item.getCount();
		}

		return sum * discount;
	}

	private double getDiscount(String role) {
		return discountMap.get(role);
	}

	public void checkout(BookBasket bookBasket) {
		bookBasket.getBookItemList().clear();
	}

	public double sumPay(BookBasket bookBasket, Customer cust) {
		return sumPay(bookBasket, cust.getLevel());
	}

	public void checkout(BookBasket bookBasket, Customer cust) {
		int sumPay = (int) sumPay(bookBasket, cust.getLevel());
		cust.setPoint(sumPay + cust.getPoint());
		if (cust.getPoint() >= 1000) {
			cust.setLevel("SILIVERROLE");
		}
		if (cust.getPoint() >= 2000) {
			cust.setLevel("GOLDENROLE");
		}
		checkout(bookBasket);
	}
}
